//
//  SNKOtherNewsController.m
//  SleepNews
//
//  Created by Kavee DJ on 2016/12/8.
//  Copyright © 2016年 Kavee DJ. All rights reserved.
//

#import "SNKOtherNewsController.h"
#import "SNKOriginWebController.h"

#import "SNKNewsModel.h"
#import "SNKNewsTableCell.h"

#import <MJRefresh.h>
#import <MJExtension.h>
#import <DKNightVersion.h>
#import <PPNetworkHelper.h>
#import "MBProgressHUD+Extion.h"

@interface SNKOtherNewsController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic , strong) UITableView *myTableView;
@property (nonatomic , assign) NSInteger currentPage;
@property (nonatomic , strong) NSMutableArray *newsArray;
@property (nonatomic , strong) NSMutableDictionary *parameters;

@end

@implementation SNKOtherNewsController

/* cell的重用标识 */
static NSString * const SNKNewsTableCellId = @"news";
static NSString *jsBaseUrlGet = @"http://api.jisuapi.com/news/get";

#pragma mark - Life Cycle  
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.myTableView];
    // TabBar 双击通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mynotification) name:@"新闻" object:nil];
    
    //第一次直接刷新
    [self.myTableView.mj_header beginRefreshing];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

#pragma mark - TableView DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.newsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SNKNewsTableCell *cell = [tableView dequeueReusableCellWithIdentifier:SNKNewsTableCellId forIndexPath:indexPath];
    cell.dk_backgroundColorPicker = DKColorPickerWithKey(BAR);
    SNKNewsModel *news = self.newsArray[indexPath.row];
    cell.news = news;
    return cell;
}

#pragma mark - tableView Delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SNKNewsModel *newsModel = self.newsArray[indexPath.row];
    SNKOriginWebController *originVC = [[SNKOriginWebController alloc] init];
    originVC.originUrl = newsModel.weburl;
    [self.navigationController pushViewController:originVC animated:YES];
}

#pragma mark - Getter and Setter
- (NSMutableArray *)newsArray
{
    if (_newsArray == nil) {
        _newsArray = [NSMutableArray array];
    }
    return _newsArray;
}

- (UITableView *)myTableView {
    if (_myTableView == nil) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENW, SCREENH -  64 - 49)];
        _myTableView.dataSource = self;
        _myTableView.delegate = self;
        
        [_myTableView registerNib:[UINib nibWithNibName:NSStringFromClass([SNKNewsTableCell class]) bundle:nil] forCellReuseIdentifier:SNKNewsTableCellId];
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableView.dk_backgroundColorPicker = DKColorPickerWithKey(SEP);
        
        // 设置下拉刷新控件
        IMP_BLOCK_SELF(SNKOtherNewsController);
        _myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [block_self requestNewData];
        }];
        
        // 设置上拉加载更多
        _myTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [block_self requestMoreData];
        }];
    }
    return _myTableView;
}

- (NSMutableDictionary *)parameters {

    if (_parameters == nil) {
        _parameters = [NSMutableDictionary dictionary];
        
        // 不变的参数
        _parameters[@"channel"] = self.content;
        _parameters[@"num"] = @(10);
        _parameters[@"appkey"] = @"822322bfc5ef6f40";
    }
    // 变的参数
    _parameters[@"start"] = @(self.currentPage * 10);
    
    return _parameters;
}

#pragma mark - Private Methods
- (void)mynotification
{
    [self.myTableView.mj_header beginRefreshing];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"新闻" object:nil];
}

// 上拉,加载更多旧数据
- (void)requestMoreData {
    self.currentPage ++;
    [self getTableViewData];
}

// 下拉,刷新数据
- (void)requestNewData {
    // 清零
    self.currentPage = 0;
    [_newsArray removeAllObjects];
    [self getTableViewData];
}

// 纯粹的加载列表
- (void)getTableViewData {
    IMP_BLOCK_SELF(SNKOtherNewsController);
    
    [PPNetworkHelper GET:jsBaseUrlGet parameters:self.parameters responseCache:^(id responseCache) {
        if (![PPNetworkHelper isNetwork]) {
            NSArray *newsObjArray = [SNKNewsModel mj_objectArrayWithKeyValuesArray:responseCache[@"result"][@"list"]];
            [block_self.newsArray addObjectsFromArray:newsObjArray];
            [block_self.myTableView reloadData];
            [block_self.myTableView.mj_header endRefreshing];
            [block_self.myTableView.mj_footer endRefreshing];
        }
    } success:^(id responseObject) {
        NSArray *newsObjArray = [SNKNewsModel mj_objectArrayWithKeyValuesArray:responseObject[@"result"][@"list"]];
        [block_self.newsArray addObjectsFromArray:newsObjArray];
        [block_self.myTableView reloadData];
        [block_self.myTableView.mj_header endRefreshing];
        [block_self.myTableView.mj_footer endRefreshing];
    } failure:^(NSError *error) {
        [block_self.myTableView.mj_header endRefreshing];
        [block_self.myTableView.mj_footer endRefreshing];
    }];
}

@end
